import Vue from 'vue'
import Vuex from 'vuex'
import Cookie from 'vue-cookies'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {
      userId: Cookie.get('userId'),
      userName: Cookie.get('userName'),
      nickName: Cookie.get('nickName'),
      roleName: Cookie.get('roleName')
      // username: window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).username
    },
    count: Cookie.get('online'),
    roleMenu: Cookie.get('roleMenuData')
  },
  mutations: {
    login (state, user) {
      state.user.userName = user.account
      state.user.nickName = user.nickName
      state.user.roleName = user.roleName
      state.user.userId = user.user_Id

      Cookie.set('userId', user.user_Id)
      Cookie.set('userName', user.account)
      Cookie.set('nickName', user.nickName)
      Cookie.set('roleName', user.roleName)

      // window.localStorage.setItem('user', JSON.stringify(user))
    },
    loginOut (state) {
      // this.$store.state.count -= 1
      this.state.user = {}
      Cookie.remove('userName')
      Cookie.remove('nickName')
      Cookie.remove('roleName')
      Cookie.remove('userId')
      Cookie.remove('roleMenuData')
    },
    calculateOnline (state, num) {
      state.count = num
      Cookie.set('online', num)
    },
    calculateOutline (state) {
      Cookie.remove('online')
    },
    roleMenuResult (state, roleMenuData) {
      state.roleMenu = roleMenuData
      Cookie.set('roleMenuData', roleMenuData)
    }

  }
})
